package com.windystory.wslibrary.base.guide;


import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.windystory.wslibrary.R;
import com.windystory.wslibrary.base.activity.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * BaseGuideActivity导航欢迎页
 * 注意最后一个有按钮的Fragment自己构造并添加到List中
 * Created by windystory on 08/23/2019.
 */
public abstract class BaseGuideActivity extends BaseActivity {

    private ViewPager vp;
    private List<Fragment> fragmentLists = new ArrayList<>();

    @Override
    public int getLayoutRes() {
        return R.layout.activity_base_guide;
    }

    @Override
    protected boolean setEnableSwipeBack() {
        return false;
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initView() {
        vp = (ViewPager) findViewById(R.id.vp);
    }

    @Override
    protected void initData() {
        getSupportActionBar().hide();
        fragmentLists.addAll(initGuideFragments());
        ViewPageAdapter adapter = new ViewPageAdapter(getSupportFragmentManager(), fragmentLists);
        vp.setAdapter(adapter);
        initNeedData();
    }

    /**
     * 初始化需要用的数据等
     */
    protected abstract void initNeedData();

    /**
     * ViewPageAdapter适配器
     */
    public class ViewPageAdapter extends FragmentPagerAdapter {
        private List<Fragment> fragmentList;

        ViewPageAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

    }


    /**
     * 初始化Fragment
     *
     * List<Fragment> fragments = new ArrayList<>();
     * fragments.add(new BaseGuideFragment().newInstance(R.drawable.b1));
     * fragments.add(new BaseGuideFragment().newInstance(R.drawable.b2));
     * fragments.add(new BaseGuideFragment().newInstance(R.drawable.b3));
     * fragments.add(new LastGuideFragment());
     * return fragments;
     *
     *
     */
    public abstract List<Fragment> initGuideFragments();


}
